﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using PlugNT.Common;
using PlugNT.Custom;
using PlugNT.Cms.Core;
using PlugNT.Cms.Model;
using PlugNT.Cms.BLL;
using PlugNT.Cms.Page;

namespace PlugNT.Cms.Web.Admin.Pre_Field
{
    public partial class EditField : AdminPage
    {
        private Category curCategory = new Category();
        private Tables curTables = new Tables();
        private Field curField = new Field();
        private string sid = "0";

        public const string htmlType = FieldHtmlType.dropdownlistType;
        protected void Page_Load(object sender, EventArgs e)
        {
            //权限检测
            this.CheckPower();

            string type = (Request.QueryString["t"] == null) ? "" : Request.QueryString["t"].Trim();
            sid = (Request.QueryString["id"] == null) ? "0" : Request.QueryString["id"].Trim();

            if (!PlugNT.Custom.WebConfig.IsAccess)
            {
                if (!IsPostBack)
                {
                    switch (type)
                    {
                        case "add":
                            Button_update.Text = " 添加 ";
                            break;
                        case "edit":
                            Button_update.Text = " 更新 ";
                            ddlTablesPostfix.BackColor = System.Drawing.Color.Gray;
                            ddlTablesPostfix.Enabled = false;
                            txtColumn.ReadOnly = true;
                            break;
                        case "delete":
                            Delete();
                            break;
                        default:
                            Response.Redirect("../Message.aspx?message=字段绑定失败，请从正确路径入口！" + "&z=no");
                            break;
                    }
                    //绑定数据
                    InitData();
                    //操作类型
                    hidAction.Value = type;
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=字段绑定失败，Access数据库无此项功能！" + "&z=no");
            }
        }

        private void InitData()
        {
            //绑定分表后缀
            this.ddlTablesPostfix.Items.Clear();
            DataTable dt = curTables.GetList();
            foreach( DataRow dr in dt.Rows)
            {
                this.ddlTablesPostfix.Items.Add(new ListItem(dr["description"].ToString(), dr["postfix"].ToString()));
            }

            //绑定控件类型
            this.ddlHtmltype.Items.Clear();
            this.ddlHtmltype.Items.Add(new ListItem("文本框",FieldHtmlType.textType));
            this.ddlHtmltype.Items.Add(new ListItem("多行文本", FieldHtmlType.textareaType));
            this.ddlHtmltype.Items.Add(new ListItem("下拉列表", FieldHtmlType.dropdownlistType));
            this.ddlHtmltype.Items.Add(new ListItem("编辑器", FieldHtmlType.editerType));
            this.ddlHtmltype.Attributes.Add("onchange", "changeddl();");

            //绑定字段类型
            this.ddlColtype.Items.Clear();
            this.ddlColtype.Items.Add(new ListItem("字符串", FieldType.stringType));
            this.ddlColtype.Items.Add(new ListItem("整数", FieldType.integerType));
            this.ddlColtype.Items.Add(new ListItem("日期", FieldType.datetimeType));

            //绑定分类
            this.ddlBindclslist.Items.Clear();
            this.ddlBindclslist.Items.Add(new ListItem("绑定全部网站类别", ""));
            DataTable dt2 = curCategory.GetOrderList();
            foreach (DataRow dr in dt2.Rows)
            {

                int ClassTj = Convert.ToInt32(dr["clslistlen"]);
                string tmpClslist = dr["clslist"].ToString().Trim();
                string ClassName = dr["clsname"].ToString().Trim();
                ListItem li = null;
                if (ClassTj == 1)
                {
                    li = new ListItem(ClassName, tmpClslist);
                }
                else
                {
                    ClassName = "├ " + ClassName;
                    ClassName = PlugNT.Common.Utils.StringOfChar(ClassTj - 1, "　") + ClassName;
                    li = new ListItem(ClassName, tmpClslist);
                }
                this.ddlBindclslist.Items.Add(li);
            }


            if (sid != "" && sid != "0")
            {
                FieldInfo field = curField.GetFieldInfo(int.Parse(sid));
                if (field != null)
                {
                    ddlTablesPostfix.SelectedValue = field.tabpostfix.ToString();
                    txtColumn.Text = field.column;
                    ddlColtype.SelectedValue = field.coltype;
                    txtTitle.Text = field.title;
                    ddlHtmltype.SelectedValue = field.htmltype;
                    txtHtmldepth.Text = field.htmldepth;
                    txtDefaultValue.Text = field.defaultvalue;
                    ddlBindclslist.SelectedValue = field.bindclslist;
                    cbxIsbindsub.Checked = field.isbindsub;
                    txtOrder.Text = field.order.ToString();
                    txtLength.Text = field.length.ToString();
                }
            }
        }

        private void Delete()
        {
            FieldInfo field = curField.GetFieldInfo(int.Parse(sid));
            if (field != null)
            {
                if (PlugNT.Cms.Core.Table.ModifyTable(FieldActionType.delete, field))
                {
                    if (curField.Delete(field.id))
                    {
                        Response.Redirect("Admin.aspx");
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=字段删除失败！" + "&z=no");
                    }
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=字段删除失败，请手动删除["+field.column+"]字段！" + "&z=no");
                }
            }
            else
            {
                Response.Redirect("../Message.aspx?message=字段删除失败,该字段已经被删除！" + "&z=no");
            }
        }


        protected void Button_update_Click(object sender, EventArgs e)
        {
            FieldInfo field = new FieldInfo();
            field.tabpostfix = ddlTablesPostfix.SelectedValue.Trim();
            field.column = txtColumn.Text.Trim();
            field.coltype = ddlColtype.Text.Trim();
            field.title = txtTitle.Text.Trim();
            field.htmltype = ddlHtmltype.SelectedValue.Trim();
            field.htmldepth = txtHtmldepth.Text.Trim();
            field.defaultvalue = txtDefaultValue.Text.Trim();
            field.bindclslist = ddlBindclslist.SelectedValue.Trim();
            field.isbindsub = cbxIsbindsub.Checked;
            field.order = int.Parse(txtOrder.Text.Trim());
            field.length = int.Parse(txtLength.Text.Trim());

            if (hidAction.Value == "add")
            {
                if (!curField.Exists(field.column, field.tabpostfix))
                {
                    if (PlugNT.Cms.Core.Table.ModifyTable(FieldActionType.add, field))
                    {
                        if (curField.Add(field))
                        {
                            Response.Redirect("./Admin.aspx");
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=添加字段失败！&z=no");
                        }
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=添加字段失败，请手动添加[" + field.column + "]字段！&z=no");
                    }
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=添加字段失败，字段已创建！&z=no");
                }

            }
            else
            {
                if (hidAction.Value == "edit")
                {
                    field.id = int.Parse(sid);
                    if (field != null)
                    {
                        if (PlugNT.Cms.Core.Table.ModifyTable(FieldActionType.update, field))
                        {
                            if (curField.Update(field))
                            {
                                Response.Redirect("./Admin.aspx");

                            }
                            else
                            {
                                Response.Redirect("../Message.aspx?message=修改字段失败！" + "&z=no");
                            }
                        }
                        else
                        {
                            Response.Redirect("../Message.aspx?message=添加字段失败，请手动修改[" + field.column + "]字段！&z=no");
                        }
                    }
                    else
                    {
                        Response.Redirect("../Message.aspx?message=修改字段失败，字段为空！" + "&z=no");
                    }
                }
                else
                {
                    Response.Redirect("../Message.aspx?message=字段修改失败，未知操作！" + "&z=no");
                }
            }

        }

    }
}
